package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wxbean.GoodsCountVo;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/9/30 18:32
 */
@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    GoodsMapper goodMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    AttributeMapper attributeMapper;

    @Autowired
    CategoryIdMapper categoryIdMapper;

    @Autowired
    SpecificationsMapper specificationsMapper;

    @Autowired
    BrandListMapper brandListMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public ListBean queryGoodListBean(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {

        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page, limit);

        //example是提供条件的
        GoodsExample goodExample = new GoodsExample();
        //and + 列名 + 条件
        GoodsExample.Criteria criteria = goodExample.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsSn != null) {
            criteria.andGoodsSnLike(goodsSn + "%");
        }
        //criteria.andUsernameLike("%" + username + "%").andMobileEqualTo(mobile);
        //排序
        goodExample.setOrderByClause(sort + " " + order);
        List<Goods> goods = goodMapper.selectByExampleWithBLOBs(goodExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(goods);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(goods);
        //listBean.setTotal(users.size());
        listBean.setTotal(total);
        return listBean;

    }

    @Override
    public GoodVO queryGoodDetail(Integer id) {
        GoodVO goodVO = new GoodVO();

        Goods goods = goodMapper.selectByPrimaryKey(id);
        goodVO.setGoods(goods);

        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<Product> products = productMapper.selectByExample(productExample);
        goodVO.setProducts(products);

        AttributeExample attributeExample = new AttributeExample();
        AttributeExample.Criteria criteria1 = attributeExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<Attribute> attributes = attributeMapper.selectByExample(attributeExample);
        goodVO.setAttributes(attributes);

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria2 = goodsExample.createCriteria();
        criteria2.andIdEqualTo(id);
        List<Goods> goodsList = goodMapper.selectByExample(goodsExample);
        List<Integer> categoryIds = new ArrayList<>();
        for (Goods e : goodsList) {
            CategoryId categoryId = categoryIdMapper.selectByPrimaryKey(e.getCategoryId());
            if (categoryId != null) {

                Integer pid = categoryId.getPid();
                categoryIds.add(pid);
            }
            categoryIds.add(e.getCategoryId());
        }
        goodVO.setCategoryIds(categoryIds);


        SpecificationsExample specificationsExample = new SpecificationsExample();
        SpecificationsExample.Criteria criteria3 = specificationsExample.createCriteria();
        criteria3.andGoodsIdEqualTo(id);
        List<Specifications> specifications = specificationsMapper.selectByExample(specificationsExample);
        goodVO.setSpecifications(specifications);
        return goodVO;
    }

    @Override
    public BrandVO queryBrand() {
        BrandListExample brandListExample = new BrandListExample();
        List<BrandList> brandLists = brandListMapper.selectByExample(brandListExample);
        BrandVO brandVO = new BrandVO();
        brandVO.setBrandList(brandLists);

        List<CategoryList> categoryLists = categoryIdMapper.selectCategoryList();
        for (CategoryList e : categoryLists) {
            Integer value = e.getValue();
            List<Children> childrenList = categoryIdMapper.selectChildrenList(value);
            e.setChildren(childrenList);
        }
        brandVO.setCategoryList(categoryLists);
        return brandVO;
    }

    @Override
    public void updateGood(GoodVO goodVO) {
        //商品参数
        AttributeExample attributeExample = new AttributeExample();
        AttributeExample.Criteria criteria1 = attributeExample.createCriteria();
        criteria1.andGoodsIdEqualTo(goodVO.getGoods().getId());
        List<Attribute> attributes = goodVO.getAttributes();
        attributeMapper.deleteByExample(attributeExample);
        for (Attribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            attribute.setGoodsId(goodVO.getGoods().getId());
            attributeMapper.insertSelective(attribute);
        }

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        goodVO.getGoods().setUpdateTime(new Date());
        criteria.andIdEqualTo(goodVO.getGoods().getId());
        goodMapper.updateByExampleSelective(goodVO.getGoods(), goodsExample);

        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andGoodsIdEqualTo(goodVO.getGoods().getId());
        List<Product> products = goodVO.getProducts();
        for (Product product : products) {
            product.setUpdateTime(new Date());
            productMapper.updateByExampleSelective(product,productExample);
        }

        SpecificationsExample specificationsExample = new SpecificationsExample();
        specificationsExample.createCriteria().andGoodsIdEqualTo(goodVO.getGoods().getId());
        List<Specifications> specifications = goodVO.getSpecifications();
        specificationsMapper.deleteByExample(specificationsExample);
        for (Specifications specification : specifications) {
            specification.setUpdateTime(new Date());
            specification.setGoodsId(goodVO.getGoods().getId());
            specificationsMapper.insertSelective(specification);
        }

    }

    @SneakyThrows
    @Override
    public void create(GoodVO goodVO) {
        Goods goods = goodVO.getGoods();
        goods.setAddTime(new Date());
        goodMapper.insertSelective(goods);


        List<Attribute> attributes = goodVO.getAttributes();
        for (Attribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(new Date());
            attributeMapper.insertSelective(attribute);
        }



        List<Product> products = goodVO.getProducts();
        for (Product product : products) {
            product.setGoodsId(goods.getId());
            product.setAddTime(new Date());
            productMapper.insertSelective(product);
        }

        List<Specifications> specifications = goodVO.getSpecifications();
        for (Specifications specification : specifications) {
//            int id = goodMapper.createId();
            specification.setGoodsId(goods.getId());
            specification.setAddTime(new Date());
            specificationsMapper.insertSelective(specification);
        }

    }

    @Override
    public void delete(Goods good) {

        goodMapper.deleteByPrimaryKey(good.getId());

        SpecificationsExample specificationsExample = new SpecificationsExample();
        specificationsExample.createCriteria().andGoodsIdEqualTo(good.getId());
        specificationsMapper.deleteByExample(specificationsExample);

        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andGoodsIdEqualTo(good.getId());
        productMapper.deleteByExample(productExample);

        AttributeExample attributeExample = new AttributeExample();
        attributeExample.createCriteria().andGoodsIdEqualTo(good.getId());
        attributeMapper.deleteByExample(attributeExample);
    }

    @Override
    public CommentVO commentList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page, limit);

        //example是提供条件的
        CommentExample commentExample = new CommentExample();
        //and + 列名 + 条件
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        //criteria.andUsernameLike("%" + username + "%").andMobileEqualTo(mobile);
        //排序
        commentExample.setOrderByClause(sort + " " + order);
        List<Comment> commentList = commentMapper.selectByExample(commentExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(commentList);
        long total = pageInfo.getTotal();

        CommentVO commentVO = new CommentVO();
        commentVO.setItems(commentList);
        commentVO.setTotal(total);
        return commentVO;
    }

    @Override
    public void deleteComment(Comment comment) {
        commentMapper.deleteByPrimaryKey(comment.getId());
    }

    @Override
    public int reply(Integer commentId, String content) {
        Feedback feedback1 = feedbackMapper.selectByPrimaryKey(commentId);
        if(feedback1!=null){
            return 404;
        }
        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        Feedback feedback = new Feedback();
        feedback.setUserId(comment.getUserId());
        feedback.setContent(content);
        feedback.setHasPicture(comment.getHasPicture());
        //feedback.setPicUrls(String.valueOf(comment.getPicUrls()));
        feedback.setPicUrls(comment.getPicUrls());
        feedback.setAddTime(new Date());
        feedback.setDeleted(comment.getDeleted());
        feedbackMapper.insertSelective(feedback);
        return 200;
    }

    @Override
    public GoodsCountVo count() {
        GoodsExample goodsExample = new GoodsExample();
        long count = goodMapper.countByExample(goodsExample);
        GoodsCountVo goodsCountVo = new GoodsCountVo();
        goodsCountVo.setGoodsCount((int) count);
        return goodsCountVo;
    }
}
